<?php
//游客点评类
class Controller_WWW_TouristComments extends Controller_WWW_Template
{
    public  $path;
    public function __construct ($req, $res)
    {
        $this->path = "upload/tourist_pic/";
        $this->template = "smarty:www/tourist_comments/index";
        parent::__construct($req, $res);
    }

    public function action_index($page = 1){
        $this->action_line_comment_list($page);
    }

    public function action_line_comment_list($page = 1){
        $uid = $this->_uid;
        $page     = max(1, $page);
        $limit    = 10;
        $offset   = $limit * ($page - 1);
        list($list,$counts) = User_TouristComments::track_info($limit,$offset,$uid);
        $pagination = $this->pagination($counts, $limit);

        $this->template->set('pagination', $pagination);
        $this->template->set('list', $list);
        $this->template->set('count', count($list));
        $this->template->set('right', "track_list");
    }

    public function action_add_line_comment($id){
        $post = $this->request->post();
        if(!empty($post)){
            list($file,$flag) = User_TouristComments::add_resource($_FILES['tourist_pic'],$this->path);
            if($flag){
                $data['tourist_pic'] = $file;
                $data['track_score'] = Arr::get($post,'track_score');
                $data['guider_score'] = Arr::get($post,'guider_score');
                $data['comments'] = htmlspecialchars($post['comments']) ;
                $data['created'] = time() ;
                User_TouristComments::tourist_track_update($data,'id',$post['id']);
                $this->request->redirect("/touristcomments/line_comment_list");
            }
        }else{
            $data = User_TouristComments::tourist_track_check_out('id',$id)->current();
            $this->template->set('data', $data);
        }
        $this->template->set('right', "line_comment");
    }

    public function action_show_line_comment($id){
        $data = User_TouristComments::tourist_track_check_out('id',$id)->current();
        $tourist_pic = explode(';',trim($data['tourist_pic'],';'));

        $this->template->set('data', $data);
        $this->template->set('tourist_pic', $tourist_pic);
        $this->template->set('right', "show_line_comment");
    }

    public function action_update_line_comment($id){
        $post = $this->request->post();
        if(!empty($post)){
            if($_FILES['tourist_pic']['name'][0]){
                list($file,$flag) = User_TouristComments::add_resource($_FILES['tourist_pic'],$this->path);
            }
            if(isset($file)){
                $data['tourist_pic'] = $file;
            }
            $data['track_score'] = $post['track_score'];
            $data['guider_score'] = $post['guider_score'];
            $data['comments'] = htmlspecialchars($post['comments']) ;
            $data['created'] = time() ;
            User_TouristComments::tourist_track_update($data,'id',$post['id']);
            $this->request->redirect("/touristcomments/line_comment_list");
        }else{
            $data = User_TouristComments::tourist_track_check_out('id',$id)->current();
            $this->template->set('data', $data);
        }
        $this->template->set('right', "line_comment");
    }


    public function action_activity_comment_list($page=1){
        $uid = $this->_uid;
        $page     = max(1, $page);
        $limit    = 10;
        $offset   = $limit * ($page - 1);
        list($list,$counts) = User_TouristComments::activity_info($limit,$offset,$uid);
        $pagination = $this->pagination($counts, $limit);

        $this->template->set('pagination', $pagination);
        $this->template->set('list', $list);
        $this->template->set('count', count($list));
        $this->template->set('right', "activity_list");

    }


    public function action_add_activity_comment($id){
        $post = $this->request->post();
        if(!empty($post)){
            list($file,$flag) = User_TouristComments::add_resource($_FILES['tourist_pic'],$this->path);
            if($flag){
                $data['tourist_pic'] = $file;
                $data['created'] = time();
                $data['activity_score'] = Arr::get($post,'activity_score');
                $data['guider_score'] = Arr::get($post,'guider_score');
                $data['comments'] = htmlspecialchars($post['comments']) ;
                User_TouristComments::tourist_activity_update($data,'id',$post['id']);
                $this->request->redirect("/touristcomments/activity_comment_list");
            }
        }else{
            $data = User_TouristComments::tourist_activity_check_out('id',$id)->current();
//            var_dump($data);
            $this->template->set('data', $data);
        }
        $this->template->set('right', "activity_comment");
    }

    public function action_show_activity_comment($id){
        $data = User_TouristComments::tourist_activity_check_out('id',$id)->current();
        $tourist_pic = explode(';',trim($data['tourist_pic'],';'));

        $this->template->set('data', $data);
        $this->template->set('tourist_pic', $tourist_pic);
        $this->template->set('right', "show_activity_comment");
    }

    public function action_update_activity_comment($id){
        $post = $this->request->post();
        if(!empty($post)){
            if($_FILES['tourist_pic']['name'][0]){
                list($file,$flag) = User_TouristComments::add_resource($_FILES['tourist_pic'],$this->path);
            }
            if(isset($file)){
                $data['tourist_pic'] = $file;
            }
            $data['activity_score'] = $post['activity_score'];
            $data['guider_score'] = $post['guider_score'];
            $data['comments'] = htmlspecialchars($post['comments']) ;
            $data['created'] = time() ;
            User_TouristComments::tourist_activity_update($data,'id',$post['id']);
            $this->request->redirect("/touristcomments/activity_comment_list");
        }else{
            $data = User_TouristComments::tourist_activity_check_out('id',$id)->current();
            $this->template->set('data', $data);
        }
        $this->template->set('right', "activity_comment");
    }


}
